﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WEB.Core.Controls;
using Ext.Net;
using WEB.Core.Util;
using WEB.BLL.Services;
using WEB.BLL.Entities;
using log4net;

namespace WEB.APP.Modules.System
{
    public partial class Permission : BasePage
    {
        public Permission()
            : base(SCREENS.PERMISSION)
        {

        }
        private static readonly ILog log = LogManager.GetLogger(typeof(Permission));

        [DirectMethod]
        public void DocumentReady()
        {
            AddListControlEdit(mmCheckAll, mmUnCheckAll);
            CheckPermissionControl();
            base.CheckPermissionHidden();
            BindingRole();
        }
        private void BindingRole()
        {
            try
            {
                RoleService roleService = RoleService.GetInstance();
                IList<RoleEntity> lstRole = roleService.LoadAll();

                foreach (var item in lstRole)
                {
                    if (item.RoleId == 1)
                    {
                        lstRole.Remove(item);
                        break;
                    }
                }

                if (CurrentUser.RoleId != 1)
                {
                    foreach (var item in lstRole)
                    {
                        if (item.RoleId == CurrentUser.RoleId)
                        {
                            lstRole.Remove(item);
                            break;
                        }
                    }
                }

                storeRole.DataSource = lstRole;
                storeRole.DataBind();

                if (CurrentUser != null)
                    if (lstRole.Count > 0)
                        cboRole.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }

        protected void storeMain_OnRefreshDate(object sender, StoreRefreshDataEventArgs e)
        {
            try
            {
                if (this.cboRole.SelectedItem.Value == null) return;

                string strRoleId = this.cboRole.SelectedItem.Value.ToString();
                int roleId = 0;
                int.TryParse(strRoleId, out roleId);
                if (roleId <= 0)
                {
                    return;
                }
                PermissionService permissionService = PermissionService.GetInstance();
                PermissionEntity permissionEntity = new PermissionEntity();
                permissionEntity.RoleId = roleId;
                IList<PermissionEntity> lstPermission = permissionService.LoadByRole(permissionEntity);
                if (lstPermission == null) return;
                storeMain.DataSource = lstPermission;
                storeMain.DataBind();
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }

        [DirectMethod]
        public void UpdateViewAll(bool IsView)
        {
            try
            {
                PermissionEntity permissionEntity = new PermissionEntity();
                permissionEntity.RoleId = int.Parse(this.cboRole.SelectedItem.Value.ToString());
                permissionEntity.IsView = IsView;
                PermissionService permissionService = PermissionService.GetInstance();
                permissionService.UpdateViewAll(permissionEntity);
                //storeMain.DataBind();
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
        [DirectMethod]
        public void UpdateAddAll(bool IsAdd)
        {
            try
            {
                PermissionEntity permissionEntity = new PermissionEntity();
                permissionEntity.RoleId = int.Parse(this.cboRole.SelectedItem.Value.ToString());
                permissionEntity.IsAdd = IsAdd;
                PermissionService permissionService = PermissionService.GetInstance();
                permissionService.UpdateAddAll(permissionEntity);
                //storeMain.DataBind();
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
        [DirectMethod]
        public void UpdateEditAll(bool IsEdit)
        {
            try
            {
                PermissionEntity permissionEntity = new PermissionEntity();
                permissionEntity.RoleId = int.Parse(this.cboRole.SelectedItem.Value.ToString());
                permissionEntity.IsEdit = IsEdit;
                PermissionService permissionService = PermissionService.GetInstance();
                permissionService.UpdateEditAll(permissionEntity);
                //storeMain.DataBind();
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
        [DirectMethod]
        public void UpdateDeleteAll(bool IsDelete)
        {
            try
            {
                PermissionEntity permissionEntity = new PermissionEntity();
                permissionEntity.RoleId = int.Parse(this.cboRole.SelectedItem.Value.ToString());
                permissionEntity.IsDelete = IsDelete;
                PermissionService permissionService = PermissionService.GetInstance();
                permissionService.UpdateDeleteAll(permissionEntity);
                //storeMain.DataBind();
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
        [DirectMethod]
        public void UpdatePrintAll(bool IsPrint)
        {
            try
            {
                PermissionEntity permissionEntity = new PermissionEntity();
                permissionEntity.RoleId = int.Parse(this.cboRole.SelectedItem.Value.ToString());
                permissionEntity.IsPrint = IsPrint;
                PermissionService permissionService = PermissionService.GetInstance();
                permissionService.UpdatePrintAll(permissionEntity);
                //storeMain.DataBind();
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
        [DirectMethod]
        public void UpdateImportAll(bool IsImport)
        {
            try
            {
                PermissionEntity permissionEntity = new PermissionEntity();
                permissionEntity.RoleId = int.Parse(this.cboRole.SelectedItem.Value.ToString());
                permissionEntity.IsImport = IsImport;
                PermissionService permissionService = PermissionService.GetInstance();
                permissionService.UpdateImportAll(permissionEntity);
                //storeMain.DataBind();
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
        [DirectMethod]
        public void UpdateExportAll(bool IsExport)
        {
            try
            {
                PermissionEntity permissionEntity = new PermissionEntity();
                permissionEntity.RoleId = int.Parse(this.cboRole.SelectedItem.Value.ToString());
                permissionEntity.IsExport = IsExport;
                PermissionService permissionService = PermissionService.GetInstance();
                permissionService.UpdateExportAll(permissionEntity);
                //storeMain.DataBind();
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }

        [DirectMethod]
        public void CheckAllRow(string ScreenId, int RoleId, bool Check)
        {
            try
            {
                PermissionEntity permissionEntity = new PermissionEntity();
                permissionEntity.ScreenId = ScreenId;
                permissionEntity.RoleId = RoleId;
                permissionEntity.IsView = permissionEntity.IsAdd = 
                    permissionEntity.IsEdit = permissionEntity.IsDelete = 
                    permissionEntity.IsPrint = permissionEntity.IsImport = 
                    permissionEntity.IsExport = Check;
                PermissionService permissionService = PermissionService.GetInstance();
                permissionService.Update(permissionEntity);
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }

        protected void grdMain_OnAfterEdit(object sender, DirectEventArgs e)
        {
            try
            {
                string ScreenId = e.ExtraParams["ScreenId"].ToString();
                int RoleId = int.Parse(e.ExtraParams["RoleId"]);
                bool IsAdd = bool.Parse(e.ExtraParams["IsAdd"]);
                bool IsView = bool.Parse(e.ExtraParams["IsView"]);
                bool IsDelete = bool.Parse(e.ExtraParams["IsDelete"]);
                bool IsEdit = bool.Parse(e.ExtraParams["IsEdit"]);
                bool IsPrint = bool.Parse(e.ExtraParams["IsPrint"]);
                bool IsImport = bool.Parse(e.ExtraParams["IsImport"]);
                bool IsExport = bool.Parse(e.ExtraParams["IsExport"]);
                PermissionEntity permissionEntity = new PermissionEntity();
                permissionEntity.ScreenId = ScreenId;
                permissionEntity.RoleId = RoleId;
                permissionEntity.IsView = IsView;
                permissionEntity.IsAdd = IsAdd;
                permissionEntity.IsDelete = IsDelete;
                permissionEntity.IsEdit = IsEdit;
                permissionEntity.IsPrint = IsPrint;
                permissionEntity.IsImport = IsImport;
                permissionEntity.IsExport = IsExport;
                PermissionService permissionService = PermissionService.GetInstance();
                permissionService.Update(permissionEntity);
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
        protected void grdMain_OnCommand(object sender, DirectEventArgs e) { }
        public void btnAdd_Click(object sender, DirectEventArgs e)
        {
            // this.WindowEditor.Show();
        }
        public void btnEdit_Click(object sender, DirectEventArgs e) { }
        public void btnDelete_Click(object sender, DirectEventArgs e) { }
        public void btnPrint_Click(object sender, DirectEventArgs e) { }
        public void btnImport_Click(object sender, DirectEventArgs e) { }
        public void btnExport_Click(object sender, DirectEventArgs e) { }
    }
}
